﻿#region License
/* Copyright (C) 2009 Tim Coulter
 * 
 * This file is part of ExtremeML.
 * 
 * ExtremeML is free software: you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation either version
 * 2 of the License, or (at your option) any later version.
 * 
 * ExtremeML is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty
 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
 * the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see http://www.gnu.org/licenses/.
*/
#endregion

using ExtremeML.Packaging;
using NUnit.Framework;

namespace ExtremeML.Tests
{
	[TestFixture]
	public class VerifyTemplatePackages
	{
		[Test]
		public void VerifyEmptyWorkbookTemplateResource()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.EmptyWorkbookTemplateResource)))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
				Assert.AreEqual(3, package.WorkbookPart.WorksheetParts.Count, "Worksheet parts count.");

				foreach (var part in package.WorkbookPart.WorksheetParts)
				{
					Assert.NotNull(part.Name, "Worksheet name.");
					Assert.AreNotEqual(part.Name, string.Empty, "Worksheet name.");
					Assert.AreEqual(0, part.Worksheet.SheetData.Rows.Count, "Worksheet row count.");
					Assert.AreEqual(0, part.TableDefinitionParts.Count, "Table count.");
				}
			}
		}

		[Test]
		public void VerifyEmptyTableNoTotalsTemplate()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
				Assert.AreEqual(4, package.WorkbookPart.WorksheetParts.Count, "Worksheet parts count.");

				var part = package.WorkbookPart.WorksheetParts["EmptyNoTotals"];

				Assert.NotNull(part.Name, "Worksheet name.");
				Assert.AreNotEqual(part.Name, string.Empty, "Worksheet name.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(3, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(1, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(3, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:H4", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void VerifyEmptyTableWithTotalsTemplate()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
				Assert.AreEqual(4, package.WorkbookPart.WorksheetParts.Count, "Worksheet parts count.");

				var part = package.WorkbookPart.WorksheetParts["EmptyWithTotals"];

				Assert.NotNull(part.Name, "Worksheet name.");
				Assert.AreNotEqual(part.Name, string.Empty, "Worksheet name.");
				Assert.AreEqual(3, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(1, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(3, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:H5", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}
	
		[Test]
		public void VerifyPopulatedTableNoTotalsTemplate()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
				Assert.AreEqual(4, package.WorkbookPart.WorksheetParts.Count, "Worksheet parts count.");

				var part = package.WorkbookPart.WorksheetParts["PopulatedNoTotals"];

				Assert.NotNull(part.Name, "Worksheet name.");
				Assert.AreNotEqual(part.Name, string.Empty, "Worksheet name.");
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:H7", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void VerifyPopulatedTableWithTotalsTemplate()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.TableSamplesTemplateResource)))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
				Assert.AreEqual(4, package.WorkbookPart.WorksheetParts.Count, "Worksheet parts count.");

				var part = package.WorkbookPart.WorksheetParts["PopulatedWithTotals"];

				Assert.NotNull(part.Name, "Worksheet name.");
				Assert.AreNotEqual(part.Name, string.Empty, "Worksheet name.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows.Count, "Row count.");
				Assert.AreEqual(2, part.Worksheet.SheetData.Rows[0].RowSpan.Begin, "First row number.");
				Assert.AreEqual(1, part.TableDefinitionParts.Count, "Table count.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstRowIndex, "Table first row index.");
				Assert.AreEqual(2, part.TableDefinitionParts[0].Table.FirstColumnIndex, "Table first column index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastRowIndex, "Table last row index.");
				Assert.AreEqual(7, part.TableDefinitionParts[0].Table.LastColumnIndex, "Table last column index.");
				Assert.AreEqual(4, part.TableDefinitionParts[0].Table.DataRowSpan, "Table data row count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.TableColumns.TableColumns.Count, "Table data column count.");
				Assert.AreEqual(6, part.TableDefinitionParts[0].Table.LastDataRowIndex, "Table last data row index.");
				Assert.AreEqual(6, part.Worksheet.SheetData.Rows[0].Cells.Count, "Header row cells count.");
				Assert.AreEqual("C3:H8", part.Worksheet.SheetDimension.CellArea.Range, "Sheet dimension range.");
				Assert.AreEqual(2, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.Begin, "Worksheet columnspan begin value.");
				Assert.AreEqual(7, part.Worksheet.ColumnSets[0].Columns[0].ColumnSpan.End, "Worksheet columnspan end value.");
			}
		}

		[Test]
		public void VerifyStyleSamplesTemplate()
		{
			using (var package = SpreadsheetDocumentWrapper.Open(Utility.CreateFileFromResource(Utility.StyleSamplesTemplateResource)))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
				Assert.AreEqual(1, package.WorkbookPart.WorksheetParts.Count, "Worksheet parts count.");

				var part = package.WorkbookPart.WorksheetParts["Sheet1"];

				Assert.NotNull(part.Name, "Worksheet name.");
				Assert.AreNotEqual(part.Name, string.Empty, "Worksheet name.");
				Assert.AreEqual(5, part.Worksheet.SheetData.Rows.Count, "Row count.");
			}
		}

		[Test]
		public void VerifyNewPackageCreation()
		{
			using (var package = SpreadsheetDocumentWrapper.Create(Utility.GetTemporaryFilePath()))
			{
				Assert.NotNull(package.WorkbookPart, "Workbook.");
				Assert.NotNull(package.WorkbookPart.WorksheetParts, "Worksheets collection.");
				Assert.AreEqual(0, package.WorkbookPart.WorksheetParts.Count, "Worksheet count.");
				Assert.NotNull(package.WorkbookPart.SharedStringTablePart, "Shared strings.");
				Assert.NotNull(package.WorkbookPart.ThemePart, "Theme.");
				Assert.NotNull(package.WorkbookPart.WorkbookStylesPart, "Stylesheet.");
			}
		}
	}
}
